package com.wct.util;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class Util {
	/** 
     * java字节码转字符串 
     * @param b 
     * @return 
     */
    public static String byte2hex(byte b) { //一个字节的数，
 
        // 转成16进制字符串
 


 
           String tmp = (java.lang.Integer.toHexString(b & 0XFF));
            if (tmp.length() == 1) {
            	tmp = "0" + tmp;
            } 
        return tmp.toUpperCase(); //转成大写
 
    }
    
    public static String byte2hex(byte[] array) { //一个字节的数，
    	 
        // 转成16进制字符串
    	String temp = "0x";
    	for (byte b : array) {
	        temp = temp + byte2hex(b);
        }
    	return temp; 
    }
    
    public Short byteToShort(String b) throws UnsupportedEncodingException{
    	byte[] array = b.getBytes("utf8");
    	Byte byte1 = new Byte(array[0]);
    	return byte1.shortValue();
    }
	public static  byte[] getActiveArray(ByteBuffer buffer)
	{
	  byte[] ret = new byte[buffer.remaining()];
	  if (buffer.hasArray())
	  {
	      byte[] array = buffer.array();
	      System.arraycopy(array, buffer.arrayOffset() + buffer.position(), ret, 0, ret.length);
	  }
	  else
	  {
	      buffer.slice().get(ret);
	  }
	  return ret;
	}
}
